<template>
  <div>
    <div>
      <el-dialog :visible.sync="addTeaData.dialogAddTea" :before-close="handleClose" size="tiny" title="添加教师">
        <el-form :model="addTeaPop" :rules="rulesAddTeaPop" ref="addTeaPop" label-width="96px">
          <el-row :gutter="41">
            <el-col :span="23">
              <span></span>
              <el-form-item label="姓名" prop="name">
                <el-popover placement="top-start" trigger="focus"
                              content="字符长度不大于12位,不得含有空格">
                <el-input v-model="addTeaPop.name" :maxlength="12" slot="reference" placeholder="请输入姓名"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="41">
            <el-col :span="23">
              <el-form-item label="注册手机号" prop="phone">
                <el-popover placement="top-start" trigger="focus"
                             content="字符长度11位">
                    <el-input v-model="addTeaPop.phone" placeholder="请输入注册手机号" :maxlength="11"
                     slot="reference"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="41">
            <el-col :span="23">
              <el-form-item label="性别">
                <el-radio-group v-model="addTeaPop.sex">
                  <el-radio label="0">男</el-radio>
                  <el-radio label="1">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button @click="resetFormdelDialogAddTea('addTeaPop')">取 消</el-button>
            <el-button type="primary" @click="addTeacher('addTeaPop')" :loading="loadingBtn">确 认</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addTeaInfor } from 'api/teacherManage';
import school from 'components/SchoolClasss/school';
import validate from '@/utils/validate';
import { mapGetters } from 'vuex';
export default {
    components: {school},
    props: {
        addTeaData: {
            default() {
                return {
                    dialogAddTea: false // 默认不显示
                }
            },
            required: true
        }
    },
    data() {
        return {
      // 添加学校弹框
      // dialogAddTea: false,
            loadingBtn: false,
            schoolLoading: false, // 模糊查找学校加载提示
            formInline: {
                schoolId: '',
                schoolNameList: [] // 学校名称列表
            },
            addTeaPop: {
                schoolName: '',
                name: '',
                phone: '',
                sex: '0'
                // role: ''
            },
            // roleArr: [
            //     {roleName: '教师', roleId: ''}
            // ],
            rulesAddTeaPop: {
                schoolName: [{ required: true, message: '请输入学校名称', trigger: 'blur' }],
                
                name: [
                     { required: true, message: '请输入姓名', trigger: 'blur' },
                      { pattern: /^[^\s]+$/, message: '请勿输入空格', trigger: 'blur' }
                ],
                phone: [...validate.phoneRequired()]
            }
        }
    },
    computed: {
      
        ...mapGetters([
            'schoolId',
            'schoolName'
        ])
    },
    watch: {
        'addTeaPop'() {
            this.addTeaPop.schoolName = '';
            this.addTeaPop.name = '';
            this.addTeaPop.phone = '';
            this.addTeaPop.sex = '0';
        }
    },
    created() {
        // this.getRoleList();
    },
    methods: {
      
    // 添加教师信息 确定按钮
        addTeacher(addTeaPop) {
            this.$refs[addTeaPop].validate((valid) => {
                if (valid) {// 验证通过
                    this.loadingBtn=true;
                    let schoolId = this.schoolId;
                    let name = this.addTeaPop.name;
                    let phone = this.addTeaPop.phone;
                    let sex = this.addTeaPop.sex;
                    addTeaInfor(schoolId, name, phone, sex).then(res => {
                        this.loadingBtn=false;
                        this.$message.success('添加教师成功！');
                        this.$emit('search-list');// 更新列表
                        this.handleClose();
                    }).catch(() => {
                        this.loadingBtn=false;
                    })
                } else {
                    return false;
                }
            });
        },

    // 添加教师弹出框的关闭按钮
        handleClose() {
            this.$refs.addTeaPop.resetFields();
            this.clearDate();
        },
    // 取消按钮 还原弹窗信息
        resetFormdelDialogAddTea(dataForm) {
            this.$refs[dataForm].resetFields();
            this.clearDate();
        },
        clearDate() {
            this.addTeaData.dialogAddTea = false;
            // this.addTeaPop.schoolName = '';
            // this.addTeaPop.name = '';
            // this.addTeaPop.phone = '';
            // this.addTeaPop.sex = '0';
        }
    }
}
</script>
<style lang="scss" scoped>
@import '../style/dialog.scss';
</style>


